﻿using DatabaseCore;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Export.Ui.ViewModel.Admin
{
    public class UserDetialsViewModel
    {
        private bool _isNewUser;
        private tbl_User _existUser;
        private UserDao _userDao = new UserDao();
        private RoleDao _roleDao = new RoleDao();
        private ObservableCollection<tbl_Role> _roles;

        public UserDetialsViewModel(UserMaintananceViewModel umvm)
        {
            _existUser = new tbl_User();
            _isNewUser = true;
            UMVM = umvm;
            initialData();
            initialCommand();
        }

        public UserDetialsViewModel(tbl_User existUser , UserMaintananceViewModel umvm)
        {
            _existUser = existUser;
            _isNewUser = false;
            UMVM = umvm;
            initialData();
            initialCommand();
        }

        public UserMaintananceViewModel UMVM { get; set; }
        public RelayCommand SaveCommand { get; set; }
        public RelayCommand CloseCommand { get; set; }
        public ObservableCollection<tbl_Role> Roles
        {
            get
            {
                _roles = _roles ?? new ObservableCollection<tbl_Role>();
                return _roles; 
            }
            set
            {
                _roles = value;
            }
        }

        public string UserName { get { return _existUser.UserName; } set { _existUser.UserName = value; } }
        public string Password { get { return _existUser.Password; } set { _existUser.Password = value; } }
        public string Email { get { return _existUser.Email; } set { _existUser.Email = value; } }
        public string Name { get { return _existUser.FullName; } set { _existUser.FullName = value; } }
        public int RoleId { get { return _existUser.Role; } set { _existUser.Role = value; } }
        public string Telephone { get { return _existUser.Telephone; } set { _existUser.Telephone = value; } }
        public string Fax { get { return _existUser.Fax; } set { _existUser.Fax = value; } }
        public string PinCode { get { return _existUser.PinCode; } set { _existUser.PinCode = value; } }
        public bool Active { get { return _existUser.Active; } set { _existUser.Active = value; } }

        private void initialCommand()
        {
            SaveCommand = new RelayCommand(Save);
            CloseCommand = new RelayCommand(Close);
        }

        private void initialData()
        {
            foreach (tbl_Role role in _roleDao.GetAllRole())
            {
                Roles.Add(role);
            };
        }

        private void Save(object parameter)
        {
            bool result;
            if (_isNewUser)
            {
                result = _userDao.Insert(_existUser);
            }
            else
            {
                result = _userDao.Update(_existUser);
            }
            if (result)
            {
                MessageBox.Show("Save user success");

            }
        }

        private void Close(object parameter)
        {
            UMVM.dialog.Close();
        }
    }
}
